import { HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './users.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
  ) {}

  async create(createUserDto: CreateUserDto) {
    const { username, phone } = createUserDto;
    const isUser = await this.usersRepository.findOne({ username });
    if (isUser) {
      return {
        code: HttpStatus.INTERNAL_SERVER_ERROR,
        message: '用户名已经存在！',
      };
    }

    const isPhone = await this.usersRepository.findOne({ phone });
    if (isPhone) {
      return {
        code: HttpStatus.INTERNAL_SERVER_ERROR,
        message: '手机号已存在！',
      };
    }
    return this.usersRepository
      .save(createUserDto)
      .then(() => {
        return {
          code: HttpStatus.OK,
          message: '创建成功！',
        };
      })
      .catch((error) => {
        return {
          code: HttpStatus.INTERNAL_SERVER_ERROR,
          message: error.message,
        };
      });
  }

  async findAll() {
    const data = await this.usersRepository.find();
    return {
      code: HttpStatus.OK,
      data,
    };
  }

  async findOne(id: number) {
    const data = await this.usersRepository.findOne(id);
    if (!data) {
      return {
        code: HttpStatus.INTERNAL_SERVER_ERROR,
        message: '用户不存在！',
      };
    }
    return {
      code: HttpStatus.OK,
      data,
    };
  }

  async update(id: number, updateUserDto: UpdateUserDto) {
    const user = await this.usersRepository.findOne(id);

    if (!user) {
      return {
        code: HttpStatus.INTERNAL_SERVER_ERROR,
        message: '用户不存在！',
      };
    }

    const { username, password, name, phone } = updateUserDto;
    user.username = username;
    user.password = password;
    user.name = name;
    user.phone = phone;
    return this.usersRepository
      .save(user)
      .then(() => {
        return {
          code: HttpStatus.OK,
          message: '修改成功！',
        };
      })
      .catch((error) => {
        return {
          code: HttpStatus.INTERNAL_SERVER_ERROR,
          message: error.message,
        };
      });
  }

  async remove(id: number) {
    const user = await this.usersRepository.findOne(id);
    if (!user) {
      return {
        code: HttpStatus.INTERNAL_SERVER_ERROR,
        message: '用户不存在！',
      };
    }
    return this.usersRepository
      .remove(user)
      .then(() => {
        return {
          code: HttpStatus.OK,
          message: '删除成功！',
        };
      })
      .catch((error) => {
        return {
          code: HttpStatus.INTERNAL_SERVER_ERROR,
          message: error.message,
        };
      });
  }

  async findOneValite(username: string): Promise<User | undefined> {
    const user = await this.usersRepository.findOne({ username: username });
    return user;
  }
}
